const { readFileSync } = require('fs');
const normalPxReg = /(\d+(\.\d+)?)px/g;
const commentPxReg = /(\d+(\.\d+)?);?\/\*px\*\/$/gm;
const template = readFileSync('src/demo.js', "utf-8");

const options = {
  remUnit: 75,
  remPrecision: 5
}

// console.log('match', ...template.matchAll(commentPxReg));

const newContents = template.replace(commentPxReg, (source, g1) => {
  console.log('g1', g1);
  if (!isNaN(+g1)) {
    return calcValue(g1 + 'px');
  }
  return source;
});
console.log('newContents', newContents);

function calcValue(value) {
  const {remUnit, remPrecision} = options;
  return value.replace(normalPxReg, (_, $1) => {
    let val = (+$1 / remUnit).toFixed(remPrecision);
    return val + 'rem';
  });
}
